/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.MaintainCheckContent;
import org.springblade.modules.repair.entity.MaintainCheckType;
import org.springblade.modules.repair.entity.MaintainMalfunctionHistory;
import org.springblade.modules.repair.entity.MaintainPart;
import org.springblade.modules.repair.service.IMaintainCheckContentService;
import org.springblade.modules.repair.service.IMaintainCheckTypeService;
import org.springblade.modules.repair.service.IMaintainMalfunctionHistoryService;
import org.springblade.modules.repair.service.IMaintainPartService;
import org.springblade.modules.repair.vo.MaintainCheckContentVO;
import org.springblade.modules.repair.vo.plan.CheckContentNodeOneVO;
import org.springblade.modules.repair.vo.plan.CheckContentNodeTwoVO;
import org.springblade.modules.repair.vo.plan.CheckContentVO;
import org.springblade.modules.repair.wrapper.MaintainCheckContentWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 维保质量检查内容 控制器
 *
 * @author BladeX
 * @since 2021-05-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/maintain/checkcontent")
@Api(value = "部件管理", tags = "部件管理")
public class MaintainCheckContentController extends BladeController {

	private final IMaintainCheckContentService maintainCheckContentService;

	private final IMaintainCheckTypeService maintainCheckTypeService;

	private final IMaintainPartService maintainPartService;


	private final IMaintainMalfunctionHistoryService maintainMalfunctionHistoryService;

	/**
	 * 分页 维保质量检查内容
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入maintainCheckContent")
	public R<IPage<MaintainCheckContentVO>> list(Long planId) {
		MaintainCheckContent maintainCheckContent = new MaintainCheckContent();
		Query query = new Query();
		query.setSize(100);
		IPage<MaintainCheckContent> pages = maintainCheckContentService.page(Condition.getPage(query), Condition.getQueryWrapper(maintainCheckContent));
		List<MaintainMalfunctionHistory> list = maintainMalfunctionHistoryService.list(Wrappers.<MaintainMalfunctionHistory>lambdaQuery().eq(MaintainMalfunctionHistory::getPlanId, planId));
//		if (list.size() > 0) {
//			list.forEach(o -> {
//				Optional<MaintainCheckContent> first = pages.getRecords().stream().filter(record -> record.getId().equals(o.getCheckContentId())).findFirst();
//				if (first.isPresent()) {
//					pages.getRecords().remove(first.get());
//				}
//			});
//		}
		return R.data(MaintainCheckContentWrapper.build().pageVO(pages));
	}


	@GetMapping("/tree/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "获取部件树形结构")
	public R<List<CheckContentVO>> getCheckContentTree() {
		List<CheckContentVO> resultList = new ArrayList<>(16);
		List<MaintainCheckType> list = maintainCheckTypeService.list();
		List<MaintainPart> maintainPartList = maintainPartService.list();
		List<MaintainCheckContent> maintainCheckContents = maintainCheckContentService.list();
		Map<Long, List<MaintainPart>> listMap = maintainPartList.stream().collect(Collectors.groupingBy(MaintainPart::getLocationId));
		Map<Long, List<MaintainCheckContent>> longListMap = maintainCheckContents.stream().collect(Collectors.groupingBy(MaintainCheckContent::getPartId));
		for (MaintainCheckType maintainCheckType : list) {
			CheckContentVO checkContentVO = new CheckContentVO();
			checkContentVO.setId(maintainCheckType.getId());
			checkContentVO.setName(maintainCheckType.getName());
			List<CheckContentNodeOneVO> nodeOneList = new ArrayList<>(16);
			List<MaintainPart> maintainParts = listMap.get(maintainCheckType.getId());
			for (MaintainPart maintainPart : maintainParts) {
				CheckContentNodeOneVO checkContentNodeOneVO = new CheckContentNodeOneVO();
				checkContentNodeOneVO.setId(maintainPart.getId());
				checkContentNodeOneVO.setName(maintainPart.getName());
				nodeOneList.add(checkContentNodeOneVO);
				List<CheckContentNodeTwoVO> nodeTwoList = new ArrayList<>(16);
				List<MaintainCheckContent> maintainCheckContentList = longListMap.get(maintainPart.getId());
				for (MaintainCheckContent maintainCheckContent : maintainCheckContentList) {
					CheckContentNodeTwoVO checkContentNodeTwoVO = new CheckContentNodeTwoVO();
					checkContentNodeTwoVO.setId(maintainCheckContent.getId());
					checkContentNodeTwoVO.setName(maintainCheckContent.getName());
					nodeTwoList.add(checkContentNodeTwoVO);
				}
				checkContentNodeOneVO.setList(nodeTwoList);
			}
			checkContentVO.setList(nodeOneList);
			resultList.add(checkContentVO);
		}
		return R.data(resultList);
	}
}
